*****************************************************
***Building block 1
***Objective: Construction of census_def_APSR.dta
***Data source: census_raw_APSR.dta- this initial dataset appends the csv sources from the various questionnaires and only focuses on the variables that are used for the analysis.
******************************************************

use census_raw_APSR, clear

ge d_grin=(grinmem=="Oui")
la var d_grin "Grin member"

ge d_male=(sex=="Homme")
la var d_male "Is male"

format reltohhhead %30s
replace reltohhhead=lower(trim(reltohhhead))
ge d_head=(strmatch(reltohhhead,"chef de m?*"))
la var d_head "Is the head of hh"

ge eldest_son=(oldestperson1infamily=="Oui")
la var eldest_son "Individual is the eldest son"

ge nn=1
bysort hhid: egen hh_size=total(nn)
la var hh_size "N. of hh members 18-45"
drop nn

ge d_couple=(strmatch(marstatus,"mari*")|strmatch(marstatus,"Concub*"))
la var d_couple "Lives in couple"

ge d_educ_1=(strmatch(educ,"aucune*"))
la var d_educ_1  "Schooling: no education"

ge d_educ_2=(strmatch(educ,"*coranique*")|strmatch(educ,"*primaire*"))
la var d_educ_2  "Schooling: basic or religious"

ge d_educ_3=(strmatch(educ,"*secondaire*")|strmatch(educ,"*technique*"))
la var d_educ_3  "Schooling: secondary school"

ge d_educ_4=(strmatch(educ,"*professionn*")|strmatch(educ,"*sup?rieure*")|strmatch(educ,"*sup??rieure*"))
la var d_educ_4  "Schooling: professional/university"

ge ind_educ=.
la var ind_educ "Level of schooling"
forvalues i=1/4{
replace ind_educ=`i' if d_educ_`i'==1
}

ge d_job=(jobinlastmo=="Oui")
la var d_job "Has worked in the last month"

ge tg_job_formal=0
la var tg_job_formal "Job in the formal sector"

cap drop x
ge x=strmatch(typeofactivity, "*Secteur formel*")
replace tg_job_formal=1 if x==1
cap drop x
ge x=strmatch(typeofactivity, "*agent*")
replace tg_job_formal=1 if x==1
cap drop x
ge x=strmatch(typeofactivity, "*campagne*")
replace tg_job_formal=1 if x==1
cap drop x
ge x=strmatch(typeofactivity, "*ecole*")
replace tg_job_formal=1 if x==1
cap drop x
ge x=strmatch(typeofactivity, "*enseignement*")
replace tg_job_formal=1 if x==1
cap drop x
ge x=strmatch(typeofactivity, "*agence*")
replace tg_job_formal=1 if x==1
cap drop x
ge x=strmatch(typeofactivity, "*pompice*")
replace tg_job_formal=1 if x==1
cap drop x
ge x=strmatch(typeofactivity, "*stagiaire*")
replace tg_job_formal=1 if x==1
cap drop x
ge x=strmatch(typeofactivity, "*rinaiee*")
replace tg_job_formal=1 if x==1
cap drop x
ge x=strmatch(typeofactivity, "*millitaire*")
replace tg_job_formal=1 if x==1
cap drop x

replace ethnicity=lower(trim(ethnicity))
replace ethnicity="" if ethnicity=="nsp"|ethnicity=="nvpr"|strmatch(ethnicity,"*coranique*")|strmatch(ethnicity,"*profess*")|strmatch(ethnicity,"*second*")|strmatch(ethnicity,"*sup?rieur*")|strmatch(ethnicity,"*techniqu*")|strmatch(ethnicity,"*aucune*")
replace ethnicity="arabe/bella/tamasheq" if ethnicity=="bela"|ethnicity=="bella" |ethnicity=="arabes"|ethnicity=="tamasheq"|ethnicity=="bella/tamasheq"
replace ethnicity="other" if inlist(ethnicity, "autre","cheriff","cretien", "cretiene","cretien","dafing", "diaka","djiokorame","edo nigeria")
replace ethnicity="other" if inlist(ethnicity,"gana","haoussa","hayoussa","kounta","samoko","sirifi")
replace ethnicity="other" if inlist(ethnicity,"sousou","wolof","yaoussa","yoroba")
replace ethnicity="other" if strmatch(ethnicity,"sak?")
replace ethnicity="foregeron" if inlist(ethnicity,"forgeron","noumou/ forgeron")
replace ethnicity="peul" if inlist(ethnicity,"foulani / peulh","grios peulh","griote")
replace ethnicity="khassonke" if strmatch(ethnicity,"*khassonk*")
replace ethnicity="malinke" if strmatch(ethnicity,"*malink*")
replace ethnicity="senufo/mianka" if inlist(ethnicity,"maniga","maraka","mianka","minianka","senufo")
replace ethnicity="mossi" if inlist(ethnicity,"monssi","mosi","mossi")
replace ethnicity="soninke" if strmatch(ethnicity,"*sonink*")
replace ethnicity="sonhrai" if strmatch(ethnicity,"*sonrha*")

local lab1 "bambara"
local lab2 "bobo"
local lab3 "bozo"
local lab4 "dogon"
local lab5 "foregeron"
local lab6 "khassonke"   
local lab7 "malinke"
local lab8 "senufo/mianka"    
local lab9 "soninke"
local lab10 "mossi"
local lab11 "peul"
local lab12 "somono"
local lab13 "sonhrai"
local lab14 "arabe/bella/tamasheq"
local lab15 "other"

local l1 bamb
local l2 bobo
local l3 bozo
local l4 dogon
local l5 foreg
local l6 khass
local l7 mal
local l8 sen_mi
local l9 son
local l10 mos
local l11 peul
local l12 somon
local l13 song
local l14 arbetam
local l15 other

forvalues i=1/15{
ge eth_`l`i''=(inlist(ethnicity,"`lab`i''"))
la var eth_`l`i'' "Ethnic group: `lab`i''"
}

replace eth_other=1 if eth_mos==1

ge d_bko=locatio=="Bamako"

keep hhid persnum d_grin d_male d_head eldest_son hh_size d_couple d_educ_2 d_educ_3 d_educ_4 d_job tg_job_formal  eth_bamb eth_bobo eth_bozo eth_dogon eth_foreg eth_khass eth_mal eth_sen_mi eth_son eth_mos eth_peul eth_somon eth_song eth_arbetam eth_other d_bko

save census_def_APSR2, replace

**************************************************************************************************************************************************************************
***Building block 2
***Objective: Construction of final_trust_surveyAPSR.dta; 
** Source: trust_survey_rawAPSR.dta: the starting dataset joins information from the survey and game modules in Bamako, Mopti, played in grins and in public spaces

use trust_survey_rawAPSR, clear

ge treat_1=(tg_treatment=="T1")
ge treat_2=(tg_treatment=="T2"|tg_treatment=="t2")
ge treat_3=(tg_treatment=="T3")
la var treat_1 "TG treatment 1"
la var treat_2 "TG treatment 2"
la var treat_3 "TG treatment 3"

replace tg_treatment="T2" if tg_treatment=="t2"

ge env1=(inlist(data_origin, "mopti members in mkt, supplement","mopti non-members","bko non-members"))
la var env1 "Games played at market/public place"

ge env2=(inlist(data_origin, "bko members","mopti members"))
la var env2 "Games played during grin meeting"

ge sh_tg_contr=tg_game_contrib/in_endown
la var sh_tg_contr "TG contribution, as a share of initial endownment"

ge d_tg_contrib=sh_tg_contr>0
la var d_tg_contrib "Contributed in TG"

ge d_tg_contrib_half=sh_tg_contr>=0.5
la var d_tg_contrib_half "Contributed at least a half in TG"

ge tg_d_female=(tg_sex=="Femme")
la var tg_d_female "Player is female"

gen tg_result_lang_lower =lower(tg_result_lang)
replace tg_result_lang_lower="peul" if tg_result_lang_lower=="peulh"
la var  tg_result_lang_lower "TG player's language"

ge minority_lang=tg_result_lang_lower!="bambara" 
la var minority_lang "Minority language"

replace tg_mar_stat=trim(lower(tg_mar_stat))
ge tg_d_single=(tg_mar_stat=="celibataire")
la var tg_d_single "The player is single"

ge in_couple=1-tg_d_single
la var in_couple "Lives in couple"

replace tg_educ=trim(lower(tg_educ))
cap drop x
ge x=strmatch(tg_educ, "*aucune*")
ge tg_educ_1=(x==1)
la var tg_educ_1  "Schooling: no education"

drop x 
ge x=strmatch(tg_educ, "*alpha*")
ge x1=strmatch(tg_educ, "*coranique*")
ge x2=strmatch(tg_educ, "*1*")
ge tg_educ_2=(x==1|x1==1|x2==1)
la var tg_educ_2  "Schooling: basic or religious"

drop x x1 x2
ge x=strmatch(tg_educ, "*2*")
ge x1=strmatch(tg_educ, "*lyc*")
ge tg_educ_3=(x==1|x1==1)
la var tg_educ_3  "Schooling: secondary/high school"

drop x x1
ge x=strmatch(tg_educ, "*profess*")
ge x1=strmatch(tg_educ, "*univers*")
ge tg_educ_4=(x==1|x1==1)
la var tg_educ_4  "Schooling: professional/university"
drop x x1

ge tg_ec_act_part=(tg_ec_act=="Oui temps partiel")
la var tg_ec_act_part "Income generating activity, last month: part-time"
replace tg_ec_act_part=. if tg_ec_act=="NVPR"

ge tg_ec_act_full=(tg_ec_act=="Oui, temps plein")
la var tg_ec_act_full "Income generating activity, last month: full-time"
replace tg_ec_act_full=. if tg_ec_act=="NVPR"

ge d_ec_act=(tg_ec_act_full==1|tg_ec_act_part==1)
la var d_ec_act "Has income generating activity"

ge tg_d_riskaverse_small=(tg_risk_1=="A")
la var tg_d_riskaverse_small "Risk averse, small stakes"

local yes_no1 tg_lend_6 tg_bank_account tg_imf_account tg_rosca tg_sav_other tg_grin tg_north_hh tg_op_selfish tg_wed_othlang tg_wed_othrel tg_wed_otheth 
local yes_no `yes_no1' `yes_no2' `yes_no3'
foreach y of local yes_no{
replace `y'=lower(trim(`y'))
replace `y'="1" if `y'=="oui"
replace `y'="0" if `y'=="non"
replace `y'="" if `y'=="nsp"
replace `y'="" if `y'=="nvpr"
destring `y', replace
}

replace tg_lend_6=0 if tg_lend_6==.
replace tg_bank_account=0 if tg_bank_account==.
replace tg_rosca=0 if tg_rosca==.
replace tg_sav_other=0 if tg_sav_other==.

ge tg_d_sav=(tg_bank_account==1| tg_imf_account==1| tg_sav_other==1| tg_rosca==1)
la var tg_d_sav "Use saving tool"

replace tg_hh_size=tg_hh_size/10 if tg_hh_size==140|tg_hh_size==200
replace tg_hh_size=. if tg_hh_size==999|tg_hh_size==99

local asset pc tv car sofa gaz mobile
foreach y of local asset{
ge d_`y'=(tg_n_`y'>0&tg_n_`y'!=.)
la var d_`y' "Household owns `y'"
}

egen asset_index=rowtotal(d_pc d_tv d_car d_sofa d_gaz d_mobile)
la var asset_index "Asset index,0-6"

ge d_mopti=strmatch(data_origin, "*mopti*")
la var d_mopti "Location: Mopti"

replace tg_transf_grin_amount=0 if tg_transf_grin_amount==.
replace tg_transf_grin_amount=tg_transf_grin_amount/10000
la var tg_transf_grin_amount "Amount for support received from grin, 10K CFA"

local pers tg_ec_supp_person6_1 tg_ec_supp_person6_2 tg_ec_supp_person12_1 tg_ec_supp_person12_2 
local i=1
foreach y of local pers{
ta `y',m
replace `y'=lower(trim(`y'))
ge pers_supp_`i' =`y' 
local i=`i'+1
}

forvalues i=1/4{
ge ecsup_pers_fr_`i'=(inlist( pers_supp_`i',"ami","ani","une amie","amie","amj"))
ge ecsup_pers_sp_`i'=(inlist( pers_supp_`i',"mari","epoux","son mari")|strmatch(pers_supp_`i',"mon fianc*"))
ge ecsup_pers_fa_`i'=(inlist( pers_supp_`i',"chef de gwa","de son pere","mon pere","un autre membre du gwa","membre eloigne de la famile","membre eloigne de la famille","grand frere en exile","membres de la famille a l exterieur"))
ge ecsup_pers_gr_`i'=(strmatch( pers_supp_`i',"*grin*"))
ge ecsup_pers_tr_`i'=(inlist( pers_supp_`i',"colleague de travail"))
}

ge ecsup_pers_fr=(ecsup_pers_fr_1==1|ecsup_pers_fr_2==1|ecsup_pers_fr_3==1|ecsup_pers_fr_4==1)
la var ecsup_pers_fr "Received economic support from friend"
ge ecsup_pers_sp=(ecsup_pers_sp_1==1|ecsup_pers_sp_2==1|ecsup_pers_sp_3==1|ecsup_pers_sp_4==1)
la var ecsup_pers_sp "Received economic support from spouse"
ge ecsup_pers_fa=(ecsup_pers_fa_1==1|ecsup_pers_fa_2==1|ecsup_pers_fa_3==1|ecsup_pers_fa_4==1)
la var ecsup_pers_fa "Received economic support from family member"
ge ecsup_pers_gr=(ecsup_pers_gr_1==1|ecsup_pers_gr_2==1|ecsup_pers_gr_3==1|ecsup_pers_gr_4==1)
la var ecsup_pers_gr "Received economic support from grin member"
ge ecsup_pers_tr=(ecsup_pers_tr_1==1|ecsup_pers_tr_2==1|ecsup_pers_tr_3==1|ecsup_pers_tr_4==1)
la var ecsup_pers_tr "Received economic support from work colleague"

ge reas_part_cohe=strmatch(tg_reas_grin_1, "*integration sociale/cohesion sociale*")
ge reas_part_cohe2=strmatch(tg_reas_grin_2, "*integration sociale/cohesion sociale*")
replace reas_part_cohe=1 if reas_part_cohe2==1
drop reas_part_cohe2
la var reas_part_cohe "Reason for grin participation: integration,cohesion"

local t friends neigh
foreach y of local t{
ta tg_h_vol_`y' ,m
replace tg_h_vol_`y'=. if tg_h_vol_`y'==999
replace tg_h_vol_`y'=tg_h_vol_`y'/10 if tg_h_vol_`y'>1000&tg_h_vol_`y'!=.
ge tg_vol_`y'_day=(tg_h_vol_`y'/24)
replace tg_vol_`y'_day=tg_vol_`y'_day/10 if tg_vol_`y'_day>21
}

la var tg_vol_neigh_day "N. of days of voluntary work in the neighbourhood"
la var tg_vol_friends_day "N. of days of voluntary work to help friends"

ge d_tg_vol_neigh=(tg_vol_neigh_day>0&tg_vol_neigh_day!=.)
ge d_tg_vol_friends=(tg_vol_friends_day>0&tg_vol_friends_day!=.)

la var d_tg_vol_neigh "Does some voluntary work in the neighbourhood
la var d_tg_vol_friends "Doese some voluntary work to help friends"

foreach u in tg_trust_north tg_trust_otheth  tg_trust_othlang tg_trust_same_lang{
ge `u'_sc=.
replace `u'_sc=0 if `u'=="pas de confiance"
replace `u'_sc=1 if `u'=="certaine confiance"
replace `u'_sc=2 if `u'=="confiance totale"
local it_label: variable label  `u'
	la var `u'_sc "`it_label'"
}

ge amount_received_B=in_endow if d_sender==0
la var amount_received_B "Amount received by sender"

ge endow_900=amount_received_B==900
ge endow_600=amount_received_B==600
la var endow_900 "Endowment received=900"
la var endow_600 "Endowment received=600"

foreach u in tg_h_vol_friends tg_h_vol_neigh    tg_vol_friends_day tg_vol_neigh_day {
replace `u'=0 if missing(`u')
}

encode tg_treatment, gen(tg_treat)

replace tg_cont_econ=tg_cont_econ*1000 if tg_cont_econ>0&tg_cont_econ<10
replace tg_cont_comm=tg_cont_comm*1000 if tg_cont_comm>0&tg_cont_comm<10
replace tg_cont_comm=tg_cont_comm*100 if tg_cont_comm>10&tg_cont_comm<100

ge d_tg_cont_econ=(tg_cont_econ>0&tg_cont_econ!=.)
ge d_tg_cont_comm=(tg_cont_comm>0&tg_cont_comm!=.)
la var d_tg_cont_comm  "Contributed to grin for: community benefits"
la var d_tg_cont_econ "Contributed to grin for: economic support of members"

drop  data_origin tg_game_contrib  tg_sex tg_result_lang tg_mar_stat tg_d_single tg_educ tg_ec_act_full tg_ec_act_part tg_ec_act tg_risk_* tg_bank_account tg_imf_account tg_rosca tg_sav_other  d_pc d_tv d_car d_sofa d_gaz d_mobile tg_n_pc tg_n_tv tg_n_car tg_n_sofa tg_n_gaz tg_n_mobile ecsup_pers_tr* ecsup_pers_fa* ecsup_pers_sp* ecsup_pers_fr* tg_ec_supp_person* tg_reas_grin_* tg_trust_north tg_trust_otheth  tg_trust_othlang amount_received_B pers_supp_*  tg_result_lang_lower ecsup_pers_gr_1 ecsup_pers_gr_2 ecsup_pers_gr_3 ecsup_pers_gr_4

save final_trust_surveyAPSR2, replace

**************************************************************************************************************************************************************************
***Building block 3
***Objective: Construction of grin_indiv_data_APSR.dta; 
** Sources: it combines the individual level cleaned daset (final_trust_surveyAPSR.dta) with a dataset with grin characteristics (grin_bko_mopti.dta). Then we clean/generate grin-level variables


***Clean grin-level survey and keep only the variables used in the analysis

use pgg_raw_APSR, clear

bysort group_id: egen group_size=count(player_id)
la var group_size "Size of the group, including all"

ta ind_eth, gen(d_eth_)

local a1 "bambara"
local a2 "malinke"
local a3 "senufo/mianka"
local a4 "dogon"
local a5 "bella/tamasheq/arabe"
local a6 "foulani/peulh/djiokorame"
local a7 "sonrhai"
local a8 "soninke/sarakole"
local a9 "khassonke"
local a10 "bobo"
local a11 "bozo"
local a12 "other"

forvalues i=1/12{
la var d_eth_`i' "Ethnic group: `a`i''"
}

forvalues i=1/12{
bysort group_id: egen n_eth_`i'=total(d_eth_`i') 
la var n_eth_`i' "N. of people of `a`i'' ethnicity"
ge sh_eth_`i'=n_eth_`i'/group_size
la var sh_eth_`i' "Share of people of `a`i'' ethnicity"
}

forvalues i=1/12{
ge sh_eth_`i'_sq=sh_eth_`i'^2
}
egen sum_sh_sq_eth=rowtotal(sh_eth_1_sq sh_eth_2_sq sh_eth_3_sq sh_eth_4_sq sh_eth_5_sq sh_eth_6_sq sh_eth_7_sq sh_eth_8_sq sh_eth_9_sq sh_eth_10_sq sh_eth_11_sq sh_eth_12_sq)
ge elf_eth=1-sum_sh_sq_eth
la var elf_eth "Ethnic diversity ELF index"

local f2 "Personnes originaires du Nord (Kidal, Tombouctou et Gao etc.)"
local ff2 "from north"

forvalues i=2/2{
ge memb_orig_`i'=(part_orig_1=="`f`i''"|part_orig_2=="`f`i''")
la var memb_orig_`i' "31. members particular origin: `ff`i''"
}

ge memb_orig_1=strmatch(part_orig_1,"*pla*")
replace memb_orig_1=1 if strmatch(part_orig_2,"*pla*")


local f1 "amis*enfance"
local ff1 "childhood friends"
local f2 "(tontine*caisse*solidarit)"
local ff2 "other group"
local f3 "m*me promotion"
local ff3 "same class"
local f4 "originaires du meme village/meme ville"
local ff4 "same place of origin"
local f5 "personne de la meme famille"
local ff5 "same family"
local f6 "personnes du meme m*tier/travail"
local ff6 "same profession"
local f7 "voisins du meme quartier"
local ff7 "neighbours"
local f8 "entre personnes de la meme mosquee/eglise"
local ff8 "same mosque/church"
local f9 "membres de meme equipe de football"
local ff9 "same sport team"

forvalues i=1/9{
ge gform_`i'=strmatch(group_form_1,"`f`i''" )|strmatch(group_form_2,"`f`i''" )|strmatch(group_form_3,"`f`i''" ) 
replace gform_`i'=. if nm_game==1
la var gform_`i' "Group formation: `ff`i''"
}

ge d_play_grin=1-nm_game
la var d_play_grin "=1 if PGG played among member of same grin, =0 if played in public place"

ge d_fin_help=1 if fin_help=="oui, avec conditions"|fin_help=="oui, sans conditions"
replace d_fin_help=0 if fin_help=="non"|fin_help=="Pas encore"
replace d_fin_help=0 if d_fin_help==.&d_play_grin==1
la var d_fin_help "Grin provides financial help"

local h  ec_serv_1 ec_serv_2 ec_serv_3
foreach y of local h{
replace `y'=ltrim(lower(`y'))
cap drop x
gen x=strmatch(`y', "*mariage*")
replace `y'="caisse aide" if x==1
cap drop x
gen x=strmatch(`y', "*c*r*monie*")
replace `y'="caisse aide" if x==1
cap drop x
gen x=strmatch(`y', "*caisse*")
replace `y'="caisse" if x==1
cap drop x
gen x=strmatch(`y', "*cr*dit*")
replace `y'="credit" if x==1
cap drop x
gen x=strmatch(`y', "*banque*")
replace `y'="credit" if x==1
}

local f1 "credit"
local ff1 "credit"
local f2 "caisse"
local ff2 "help fond"
local f3 "tontine"
local ff3 "roscas"
local f4 "aucun"
local ff4 "none"
local f5 "projet d'investissement en commun"
local ff5 "common investment"

forvalues i=1/5{
ge ecserv_`i'=(ec_serv_1=="`f`i''"|ec_serv_2=="`f`i''"|ec_serv_3=="`f`i''")
la var ecserv_`i' "Support econ. services: `ff`i''"
replace ecserv_`i' =. if nm_game==1
}

ge support_service=1-ecserv_4
la var support_service "Offer financial support service"

local var  contrib act_neigh  

foreach y of local var{
replace `y'=ltrim(lower(`y'))
replace  `y'="1" if  `y'=="oui"
replace  `y'="0" if  `y'=="non"
replace  `y'="" if  `y'=="nvpr"
replace  `y'="" if  `y'=="nsp"
destring  `y', replace
}

replace contrib=0 if contrib==.&d_play_grin==1
la var contrib "Existence of individual contributions"

local ad1 "none"
local ad2 "commerce opportunities"
local ad3 "job opportunities"

ge d_econ_adv=ec_adv_1!="aucun"&ec_adv_1!=""
replace  d_econ_adv=1 if !missing(ec_adv_2)

la var d_econ_adv "Economic advantage from grin"

replace duration=33 if duration==1982

ge d_leader=1 if name_chef_grin!="" & nm_game==0
replace d_leader=0 if name_chef_grin=="na" & nm_game==0
ta d_leader,m
la var d_leader "Grin has a leader"

ge member_entry_all=strmatch(member_entry, "*ensemble des membres")
la var member_entry_all "New members decided by all"

local f1 "Cotisations des membres"
local ff1 "Cost-sharing"

local f2 "Un volontaire"
local ff2 "one volunteer"

local f3 "le chef de grin"
local ff3 "chief of grin"

forvalues i=1/3{
ge teapayer_`i'=(tea_pay_1=="`f`i''"|tea_pay_2=="`f`i''")
la var teapayer_`i' "Tea payer: `ff`i''"
replace teapayer_`i'=. if nm_game==1
}

replace freq_meet=ltrim(lower(freq_meet))

ge mon_freq_meet=4 if freq_meet=="1 fois par semaine"|freq_meet=="chaque samedi"
replace  mon_freq_meet=1 if freq_meet=="1 fois par mois"
replace mon_freq_meet=30 if freq_meet=="chaque jour"|freq_meet=="chaque nuit"|freq_meet=="chaque soir"
replace mon_freq_meet=8 if freq_meet=="02 fois dans la semaine"|freq_meet=="durant l'ecole le week end sinon tous les jours"|freq_meet=="les weekend"|freq_meet=="les weekends"|freq_meet=="les weekens"|freq_meet=="les weekens"|freq_meet=="pendant le week end"|freq_meet=="pendant les jour ferie"
replace mon_freq_meet=12 if freq_meet=="03 fois dans la semaines"|freq_meet=="3 fois par semaine"|freq_meet=="3 fos par semaine"
replace mon_freq_meet=8 if freq_meet=="02 fois dans la semaine"|freq_meet=="2 fois dans la semaine"|freq_meet=="2 fois par semaine"|freq_meet=="2 fois par semaine mais pendant la vaccance chaque jour"|freq_meet=="2 fois par semaines"|freq_meet=="2 ou 3 fois par semaine"
replace mon_freq_meet=2 if freq_meet=="2 fois par moi"|freq_meet=="lors des fetes"|freq_meet=="lors des fetes."|strmatch(freq_meet, "par ev*ene")|strmatch(freq_meet, "*disponibil*") 
replace mon_freq_meet=3 if freq_meet=="3fois par moi"|freq_meet=="occasionnel."
replace  mon_freq_meet=20 if freq_meet=="5 fois par semaine"

la var mon_freq_meet "Monthly frequency of meetings"

ge high_freq_meet=(mon_freq_meet==30)
la var high_freq_meet "Daily grin meetings"

****generate dataset for only grin members in BKO and Mopti, 1 observation/1 group
egen tag_group=tag(group_id)
keep if tag_group==1 & d_play_grin==1
sort group_id

keep group_id elf_eth  memb_orig_1 memb_orig_2 gform_7  d_fin_help support_service contrib  d_econ_adv duration high_freq_meet d_leader member_entry_all teapayer_1  act_neigh  

la var memb_orig_1  "Presence of displaced people"
la var memb_orig_2  "Presence of people from the North"
la var act_neigh "Provide public good"
la var d_econ_adv "Economic advantage from grin"
la var duration "Duration of the grin, in years"
save grin_level_APSR, replace


**** match individual and group level datasets
use final_trust_surveyAPSR2, clear
keep if env2==1

merge m:1 group_id using grin_level_APSR 

keep if _merge==3
drop _m

keep idcode group_id elf_eth  memb_orig_1 memb_orig_2 gform_7  d_fin_help support_service contrib  d_econ_adv duration high_freq_meet d_leader member_entry_all teapayer_1 act_neigh d_tg_cont_econ  tg_cont_econ d_tg_cont_comm  tg_cont_comm  d_sender  tg_grin sh_tg_contr tg_d_female tg_age minority_lang  in_couple  tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti DG_base  reas_part_cohe  tg_transf_grin_amount ecsup_pers_gr  d_tg_contrib sh_tg_contr d_tg_contrib_half treat_1 treat_2 treat_3 

save grin_indiv_data_APSR2, replace